namespace Dida
{
	/// <summary>
	/// A 2D line segment.
	/// </summary>
	public struct Line2D
	{
		/// <summary>
		/// The start vertex of this line.
		/// </summary>
		public Vector2D start;
		/// <summary>
		/// The end vertex of this line.
		/// </summary>
		public Vector2D end;

		/// <summary>
		/// Creates a new line, initialized with the given start and end.
		/// </summary>
		/// <param name="start">The initial start position.</param>
		/// <param name="end">The initial end position.</param>
		public Line2D(Vector2D start, Vector2D end)
		{
			this.start = start;
			this.end = end;
		}

		/// <summary>
		/// Moves the start and end point of the line segment the given distance over
		/// this line's direction. If a value is positive, that means that the line
		/// will get longer in that direction.
		/// </summary>
		/// <param name="startGrow">The distance the line has to grow 
		/// at it's start position.</param>
		/// <param name="endGrow">The distance the line has to grow at 
		/// it's end position.</param>
		public void Expand(float startGrow, float endGrow)
		{
			Vector2D dir = end - start;
			dir.Normalize();

			start -= dir * startGrow;
			end += dir * endGrow;
		}

		/// <summary>
		/// Gets the normalized direction vector of this line. This direction
		/// is measured from the start vertex, and pointing in the direction of
		/// the end vertex.
		/// </summary>
		public Vector2D Direction
		{
			get
			{
				Vector2D ret = end - start;
				ret.Normalize();
				return ret;
			}
		}

		/*public bool GetIntersection(Line2D b, out Vector2D intersection)
		{
			// TODO: Test this method

			Vector2D aNormal = end - start;
			aNormal.Rotate90DegCCW();
			aNormal.Normalize();
			float aDist = -Vector2D.DotProduct(aNormal,start);

			Vector2D bDir = b.Direction;

			if (Vector2D.PerpDotProduct(aNormal, bDir) == 0)
			{
				// the lines are parallel.
				return false;
			}

			float bIntersectionPos = (Vector2D.DotProduct(b.start, aNormal) - aDist) / 
				Vector2D.DotProduct(aNormal,bDir);
			intersection = Vector2D.Interpolate(b.start, b.end, bIntersectionPos);

			if (intersection < 0 || intersection > 1)
				return false;

			return Vector2D.DotProduct(intersection - start, intersection - end) < 0;
		}*/
	}
}